package com.security.brows.room

import android.content.Context
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.forest.download.State
import com.hub.videobrowsdownload.BrowsM3U8Prepare
import com.hub.videobrowsdownload.BrowsMP4Prepare
import com.hub.videobrowsdownload.BrowsVideoPrepare
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

@Entity(tableName = "download_file")
class BrowsVideoDownloadBean {
    @PrimaryKey
    @ColumnInfo("id")
    var id:Long = 0L

    @ColumnInfo("downloadSuccessTime")
    var downloadSuccessTime:Long = 0L
    @ColumnInfo("url")
    var url:String? = null
    @ColumnInfo("webUrl")
    var webUrl:String? = null
    @ColumnInfo("thumbPath")
    var thumbPath:String? = null
    @ColumnInfo("videoWidth")
    var videoWidth:Int? = null
    @ColumnInfo("videoHeight")
    var videoHeight:Int? = null
    @ColumnInfo("videoDuration")
    var videoDuration:Double? = null
    @ColumnInfo("videoLength")
    var videoLength:Long? = null
    @ColumnInfo("isM3U8")
    var isM3U8:Boolean = false
    @ColumnInfo("stateData")
    var stateData:Int? = 0  // 0 未知状态，1 加载状态， 2加载完成状态

    @ColumnInfo("downloadState")
    var downloadState :Int = 0 // 0 waiting, 1 downloading，2 failed, 3 Stopped, 4 success
    @ColumnInfo("downloadFilePath")
    var downloadFilePath:String? = null
    @ColumnInfo("title")
    var title:String? = null
    @ColumnInfo("fileName")
    var fileName:String? = null

    fun getVideoFileSize():Long? {
        if (!downloadFilePath.isNullOrEmpty()) {
            val file = File(downloadFilePath!!)
            return file.length()
        }
        return videoLength
    }
    fun getThumbFile(context: Context): File? {
        if (thumbPath.isNullOrEmpty()) {
            return null
        }
        val thumbFile = File(thumbPath!!)
        if (thumbFile.exists()) {
            return thumbFile
        }
        val newThumbFile =
            File(thumbFile.parentFile, "download_" + thumbFile.name)
        if (newThumbFile.exists()) {
            return newThumbFile
        }
        return null
    }
    suspend fun save(context: Context, videoPrepare: BrowsVideoPrepare) {
        try {
            id = videoPrepare.getVideoId()
            url = videoPrepare.url
            webUrl = videoPrepare.webUrl
            thumbPath = videoPrepare.thumbPath.value
            videoWidth = videoPrepare.videoWidth.value
            videoHeight = videoPrepare.videoHeight.value
            videoDuration = videoPrepare.videoDuration.value
            videoLength = videoPrepare.videoLength.value
            isM3U8 = videoPrepare.isM3U8
            stateData = videoPrepare.stateLiveData.value
            title = videoPrepare.title
            fileName = videoPrepare.getFileName()
            downloadSuccessTime = videoPrepare.downloadSuccessTime

            val state = videoPrepare.downloadWrapper.value?.state
            downloadState = when (state) {
                is State.None -> {
                    0
                }

                is State.Waiting -> {
                    //    text.value = "等待中"
                    0
                }

                is State.Downloading -> {
                   1
                }

                is State.Failed -> {
                    2
                }

                is State.Stopped -> {
                    3
                }

                is State.Succeed -> {// text.value = "完成"
                   4
                }
                else -> {
                    0
                }
            }
            downloadFilePath = videoPrepare.getDownloadFile(context).absolutePath
            withContext(Dispatchers.IO){
                BrowsRoomHelper.getDao(context).insert(this@BrowsVideoDownloadBean)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    suspend fun toVideoPrepare():BrowsVideoPrepare? {
        if (url.isNullOrEmpty() ) {
            return null
        }
        val videoPrepare = if (isM3U8) BrowsM3U8Prepare(url!!, webUrl?:"") else BrowsMP4Prepare(url!!, webUrl?:"")
        withContext(Dispatchers.Main){
            videoPrepare.id = id
            videoPrepare.thumbPath.value = thumbPath
            videoPrepare.videoWidth.value = videoWidth
            videoPrepare.videoHeight.value = videoHeight
            videoPrepare.videoDuration.value = videoDuration
            videoPrepare.videoLength.value = videoLength
            videoPrepare.isM3U8 = isM3U8
            videoPrepare.stateLiveData.value = stateData
            videoPrepare.downloadFilePath = downloadFilePath
            videoPrepare.title = title
            videoPrepare.downloadSuccessTime = downloadSuccessTime
        }

        return videoPrepare
    }
}